package com.iwhalecloud.bss.kite.cucc.web.service.accept.handler.module;

import com.google.common.collect.Lists;
import com.iwhalecloud.bss.kite.client.api.scene.operate.IOperateSceneCommonService;
import com.iwhalecloud.bss.kite.client.api.scene.query.IQuerySceneCommonService;
import com.iwhalecloud.bss.kite.client.dto.inst.InstAttrDTO;
import com.iwhalecloud.bss.kite.client.vo.scene.CommonAttrModuleVO;
import com.iwhalecloud.bss.kite.client.vo.scene.CommonAttrVO;
import com.iwhalecloud.bss.kite.client.vo.scene.ModuleVO;
import com.iwhalecloud.bss.kite.client.vo.spec.ServiceOfferVO;
import com.iwhalecloud.bss.kite.common.constant.KeyConsts;
import com.iwhalecloud.bss.kite.common.constant.ServiceOfferConsts;
import com.iwhalecloud.bss.kite.common.util.KiteBeanUtils;
import com.iwhalecloud.bss.kite.common.util.KiteListUtils;
import com.iwhalecloud.bss.kite.common.util.KiteMapUtils;
import com.iwhalecloud.bss.kite.common.util.KiteStringUtils;
import com.iwhalecloud.bss.kite.cucc.client.api.attr.ILocalQuerySceneOrderAttrService;
import com.iwhalecloud.bss.kite.cucc.client.vo.scene.OrderInfoVO;
import com.iwhalecloud.bss.kite.cucc.client.vo.scene.SceneNewOfferSeneVO;
import com.iwhalecloud.bss.kite.cucc.common.constant.LocalServiceOfferConsts;
import com.iwhalecloud.bss.kite.dataservice.cache.OfferCache;
import com.iwhalecloud.bss.kite.dataservice.mapper.spec.ServiceOfferMapper;
import com.iwhalecloud.bss.kite.manager.api.IQuerySceneInstService;
import com.iwhalecloud.bss.kite.manager.enums.ActionType;
import com.iwhalecloud.bss.kite.manager.inst.KiteAttr;
import com.iwhalecloud.bss.kite.manager.inst.KiteProdInst;
import com.iwhalecloud.bss.kite.manager.inst.KiteSceneInst;
import com.iwhalecloud.bss.kite.service.util.CopyToDtoUtil;
import com.iwhalecloud.bss.kite.web.context.KiteWebContext;
import com.iwhalecloud.bss.kite.web.service.accept.bo.AcceptContext;
import com.iwhalecloud.bss.kite.web.service.accept.bo.SceneModule;
import com.iwhalecloud.bss.kite.web.service.accept.handler.module.AbstractModuleHandler;
import com.iwhalecloud.bss.kite.web.service.accept.handler.module.CommonAttrModuleHandler;
import com.ztesoft.bss.base.util.LocalAttrUtils;
import com.ztesoft.bss.common.util.SpringUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;
import java.util.stream.Collectors;

/**
 * 公共信息模块的订单信息处理
 */
@Service
public class NewOrderInfoModuleHandler  extends CommonAttrModuleHandler {

    @Autowired
    private IQuerySceneInstService querySceneInstService;
    @Autowired
    private IOperateSceneCommonService operateSceneCommonService;
    @Override
    public CommonAttrModuleVO dealExecuteSpecialAttr(AcceptContext context, SceneModule sceneModule, CommonAttrModuleVO defCommonAttrInfoVO) {
        String serviceOfferId = querySceneInstService.getSceneInst(context.getAcceptRequest().getSceneInstId()).getServiceOfferId();
        String upServiceOfferId = querySceneInstService.getExtParameterValue(context.getAcceptRequest().getSceneInstId(), "upServiceOfferId");
        String layoutPage = sceneModule.getKiteModule().getLayoutPage();
        String sceneInstId = context.getSceneVo().getSceneInstId();
        KiteSceneInst kiteSceneInst = querySceneInstService.getSceneInst(sceneInstId);
        KiteProdInst prodInst = querySceneInstService.listProdInstsFromOfferInst(context.getAcceptRequest().getCustId(), kiteSceneInst.getRootInstId()).stream()
            .filter(inst -> !KiteStringUtils.equalsAny(inst.getActionType().name(), ActionType.X.name(), ActionType.D.name()))
            .findFirst().orElse(null);
        if(LocalAttrUtils.GJ_PROD.contains(prodInst.getProdId())){
            //国际产品不做处理
            return defCommonAttrInfoVO;
        }
        //跨域规则过滤
        if(StringUtils.isNotEmpty(LocalAttrUtils.prodIdByProdId(prodInst.getProdId()))){
            if((serviceOfferId.equals(LocalServiceOfferConsts.SERVICE_OFFER_RESOURCE_CHECK)||KiteStringUtils.equalsAny(LocalServiceOfferConsts.SERVICE_OFFER_CHECK_CHANGE,upServiceOfferId,serviceOfferId))&&LocalAttrUtils.isCrossDomain(kiteSceneInst,prodInst)&&KiteStringUtils.equalsAny(layoutPage, "OrderInfo")){
                // 精品网没有这两个属性，会报错
                Boolean haveValue = defCommonAttrInfoVO.getCommonAttrList().stream().anyMatch(commonAttrVO -> {
                    return KiteStringUtils.equalsAny(commonAttrVO.getFieldName(),"ORD10013","ORD10004");
                });
                if(!haveValue){
                    return defCommonAttrInfoVO;
                }
                defCommonAttrInfoVO.getCommonAttrList().forEach(commonAttrVO -> {
                    if(KiteStringUtils.equalsAny(commonAttrVO.getFieldName(),"ORD10013","ORD10004")){
                        commonAttrVO.setShowType(KeyConsts.PROD_ATTR_SHOW_TYPE_E);
                    }
                    KiteAttr ORD10013 = SpringUtil.getBean(IQuerySceneCommonService.class).getCommonAttr(sceneInstId, "ORD10013").getResultObject();
                    ORD10013.setShowType(KeyConsts.PROD_ATTR_SHOW_TYPE_E);
                    operateSceneCommonService.setCommonAttr(sceneInstId, ORD10013);
                    KiteAttr ORD10004 = SpringUtil.getBean(IQuerySceneCommonService.class).getCommonAttr(sceneInstId, "ORD10004").getResultObject();
                    ORD10004.setShowType(KeyConsts.PROD_ATTR_SHOW_TYPE_E);
                    operateSceneCommonService.setCommonAttr(sceneInstId, ORD10004);

                });
            }else{
                KiteAttr ORD10013 = SpringUtil.getBean(IQuerySceneCommonService.class).getCommonAttr(sceneInstId, "ORD10013").getResultObject();
                if(null!=ORD10013&&KeyConsts.PROD_ATTR_SHOW_TYPE_E.equals(ORD10013.getShowType())){
                    ORD10013.setShowType(KeyConsts.PROD_ATTR_SHOW_TYPE_H);
                    operateSceneCommonService.setCommonAttr(sceneInstId, ORD10013);
                }
                KiteAttr ORD10004 = SpringUtil.getBean(IQuerySceneCommonService.class).getCommonAttr(sceneInstId, "ORD10004").getResultObject();
                if(null!=ORD10004){
                    ORD10004.setShowType(KeyConsts.PROD_ATTR_SHOW_TYPE_D);
                    operateSceneCommonService.setCommonAttr(sceneInstId, ORD10004);
                }
                CommonAttrVO vo = null;
                for (CommonAttrVO commonAttrVO :defCommonAttrInfoVO.getCommonAttrList()){
                    if(KiteStringUtils.equals(commonAttrVO.getFieldName(),"ORD10004")){
                        vo=commonAttrVO;
                        commonAttrVO.setShowType(KeyConsts.PROD_ATTR_SHOW_TYPE_D);
                    }
                }
                if(null==vo&&null!=ORD10004){
                    vo = KiteBeanUtils.transform(ORD10004,CommonAttrVO.class);
                    defCommonAttrInfoVO.getCommonAttrList().add(vo);
                }
            }
            if(defCommonAttrInfoVO.getCommonAttrList().size()>0){
                defCommonAttrInfoVO.getCommonAttrList().removeIf(
                    commonAttr -> !KiteStringUtils.equalsAny(commonAttr.getShowType(), KeyConsts.PROD_ATTR_SHOW_TYPE_D, KeyConsts.PROD_ATTR_SHOW_TYPE_M));
            }
        }
        return defCommonAttrInfoVO;
    }

    @Override
    public CommonAttrModuleVO dealPreviewSpecialAttr(AcceptContext context, SceneModule sceneModule, CommonAttrModuleVO defCommonAttrInfoVO) {
        return defCommonAttrInfoVO;
    }
}
